var oprate=[
            {"id":"updbtn","value":"修改","color":"blue","func":"update(this)"},
            {"id":"delbtn","value":"删除","color":"red","func":"del(this)"},
            {"id":"delbtn","value":"重置密码","color":"red","func":"resetpwd(this)"}];
var requestUrl="../../user/list";
var fileChangeUrl="../../user/avatar?uname=";
datatbody="datatbody";
datathead="datathead";
$(function(){
	search(datatbody,datathead);
});
//详情
function detail(obj){
	du("../../user/detail","detail",obj,function(json){
		textinit(json.user);
		$("#detail #avatar").attr("src","../"+json.user.avatar);
		$("#detail #avatar").css("width","100px");
		$("#detail #avatar").css("height","100px");
	},null,200,280);
}
//修改
function update(obj){
	var id=$(obj).parent().parent().find("id").html();
	du("../../user/detail","update",obj,function(json){
		for(var x in json.user){
			$("#update input[type=text]").each(function(i,item){
				if(item.id==x){
					$("#update #"+item.id).val(json.user[x]);
				}
			});
			$("#update #"+x).each(function(i,item){
				$(item).children().each(function(j,cite){
					$(cite).attr("selected",false);
					if(cite.value!=""&&cite.value==json.user[x]){
						$(cite).attr("selected",true);
					}
				});
			});
		}
		$("#update #description").text(json.user.description);
		$("#update #avatar").attr("src","../"+json.user.avatar);
		$("#update #avatar").css("width","100px");
		$("#update #avatar").css("height","100px");
		$("#update input[type=text]").css("width","80%");
		$("#update #description").css("width","80%");
		$("#update #description").css("height","60px");
		$("#update input[name=sex][value="+json.user["sex"]+"]").attr("checked",true); 
		$("#update input[name=sex][value="+json.user["sex"]+"]").attr("c",true); 
		fileChangeUrl=fileChangeUrl+json.user.uname;
	},function(opo,obtn,flagfunc){
		var pramjson={};
		pramjson["id"]=id;
		pramjson["sex"]=$(opo).find("input[name=sex][c=true]").val();
		pramjson["description"]=$(opo).find("#description").val();
		var avatarsrc=$(opo).find("img[id=avatar]").attr("src");
		avatarsrc=avatarsrc.substring(avatarsrc.indexOf("resources"));
		$(opo).find("input[type=text]").each(function(i,item){
			pramjson[item.id]=item.value;
		});
		$(opo).find("select").each(function(i,item){
			pramjson[item.id]=$(item).val();
		});
		pramjson["avatar"]=avatarsrc;
		$.post("../../user/update/save",pramjson,function(data){
			flagfunc(data.flag);
		})
	},200);
}
//删除
function del(obj){
	sure("确认删除吗?",function(){
		var id=$(obj).parent().parent().find("id").html();
		$.post("../../user/delete/"+id,function(data){
			if(data.flag==1){
				msg("操作成功 !",window.parent.document);
			}else{
				msg("操作失败 !",window.parent.document);
			}
			search(datatbody,datathead);
		});
	},window.parent.document,obj);
}
function resetpwd(obj){
	var id=$(obj).parent().parent().find("id").html();
	sure("确认重置密码吗?",function(){
		var id=$(obj).parent().parent().find("id").html();
		$.post("../../user/resetpwd/"+id,function(data){
			if(data.flag==1){
				msg("操作成功 !",window.parent.document);
			}else{
				msg("操作失败 !",window.parent.document);
			}
			search(datatbody,datathead);
		});
	},window.parent.document,obj);
}